/**
 * Created by BobyCo on 2017/11/21 0021.
 */
var hall = getJSONItem("hall") || {};

$(function () {
    var $headImg = $(".head-img img"),
        $bussLicenceImg = $(".buss-licence-img img"),
        $foodLicenceImg = $(".food-licence-img img"),
        isBack = sessionStorage.getItem("isBack") || 0; // 输入名字、添加标签页面返回的标识，避免页面重复从服务器加载数据
    // 初始化我的信息
    if (isBack == 0) {
        initMineInfo();
    } else {
        initViewShow();
        removeItem("isBack");
    }

    function initMineInfo() {
        /*$.post(url, {}, function (result) {
            hall = result || hall;
            initViewShow();
        }, "json");*/

        var result = {
            "headImg" : "http://img2.imgtn.bdimg.com/it/u=4039305632,4263956854&fm=27&gp=0.jpg",
            "phone": "13640778752",
            "wechat": "13640778752",
            "name": "广州瞎拌饭餐饮连锁",
            "label": "",
            "picker": "广东省 广州市 天河区",
            "pickerCodes": "440000,440100,440106",
            "bussLicenceImg": "http://img2.imgtn.bdimg.com/it/u=4039305632,4263956854&fm=27&gp=0.jpg",
            "foodLicenceImg": "http://img2.imgtn.bdimg.com/it/u=4039305632,4263956854&fm=27&gp=0.jpg"
        };
        hall = result || hall;
        initViewShow();
    }

    function initViewShow() {
        $(".name").html(hall.name);
        $(".label").html(hall.label);
        $(".picker").html(hall.picker);
        $(".phone").html(hall.phone);
        $(".wechat").html(hall.wechat);
        $headImg.attr("src", hall.headImg);
        $bussLicenceImg.attr("src", hall.bussLicenceImg);
        $foodLicenceImg.attr("src", hall.foodLicenceImg);
        // 初始化地址选择组件
        $("#city-picker").val(hall.picker).cityPicker({
            title: ""
        });
    }

    // 地址选择事件
    $('.choose-picker').off('click').click(function (e) {
        e.stopPropagation();
        $("#city-picker").picker("open");
        var pickerCodes = hall.pickerCodes || "110000,110000,110101";
        $("#city-picker").picker("setValue", pickerCodes.split(","));

        // 取消
        $(".toolbar-inner").append("<a href=\"javascript:;\" class=\"cancel-picker\">取消</a>");
        $(".cancel-picker").off("click").click(function () {
            $("#city-picker").val(hall.picker);
            $("#city-picker").picker("close");
        });
        // 完成
        $(".close-picker").off("click").click(function (e) {
            hall.picker = $("#city-picker").val()||"北京 北京市 东城区";
            hall.pickerCodes = $("#city-picker").attr("data-codes");
            initViewShow();
        });
    });
    
    // 更换头像
    $headImg.off("click").click(function () {
        $("#headImgUploader").show().trigger("click").trigger("click").hide(); // 移动端需要触发两次click
    });
    // 头像本地预览 (部分浏览器不兼容，最好是上传到服务器，重新获取地址)
    $("#headImgUploader").change(function () {
        loadImg($(this), $headImg);
    });

    // 更换营业执照
    $bussLicenceImg.off("click").click(function () {
        $("#bussLicenceUploader").show().trigger("click").trigger("click").hide();
    });
    // 营业执照本地预览
    $("#bussLicenceUploader").change(function () {
        loadImg($(this), $bussLicenceImg);
    });

    // 更换食品经营许可证
    $foodLicenceImg.off("click").click(function () {
        $("#foodLicenceUploader").show().trigger("click").trigger("click").hide();
    });
    // 食品经营许可证本地预览
    $("#foodLicenceUploader").change(function () {
        loadImg($(this), $foodLicenceImg);
    });
});

// 页面切换时保存数据
$(window).unload(function () {
    setJSONItem("hall", hall);
    // 存服务器
    /*$.post(url, hall, function (result) {

    }, "json");*/
});